
export const state = () => {
    return {
        userInfo: {
            token: "",
            user: ""
        }
    }
}
export const mutations = {
    setInfo(state, data) {
        state.userInfo = data;
    },
    clearUserInfo(state) {
        if (process.browser) {          //这里的process是当前环境下的一个对象  browser是一个布尔值，如果在浏览器环境下就为true，如果在服务器环境，就为false
            localStorage.removeItem("userInfo");
        }
        //将vuex中的数据清空
        state.userInfo = {};

    }
}
export const actions = {
    login({ commit }, data) {
        // 发送ajax请求
        return this.$axios({
            url: "/accounts/login",
            method: "post",
            data
        }).then(res => {
            console.log("res", res);
            const data = res.data;
            if (res.status == 200) {
                commit("setInfo", data);
                return res;
            }
        })
    },
    // 注册
    register({ commit }, data) {
        return this.$axios({
            url: "/accounts/register",
            method: "post",
            data
        }).then(res => {
            console.log("res", res);
            return res;
        })
    }
}